<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta http-equiv="Pragma" content="no-cache">
<meta http-equiv="Cache-Control" content="no-cache">
<meta http-equiv="Expires" content="0">
<title>登录</title>
<link href="css/login.css" type="text/css" rel="stylesheet">
</head>
<body>

	<div class="login">
		<div class="message">办公室智能管理系统</div>
		<div id="darkbannerwrap"></div>

		<form id="login-form" method="post" onsubmit="return false;">
			<input id="username" name="username" placeholder="用户名" type="text"
				autocomplete="off">
			<hr class="hr15">
			<input id="password" name="password" placeholder="密码" type="password"
				autocomplete="off">
			<hr class="hr15">
			<input id="checkCode" name="checkCode" placeholder="请输入验证码" type="text"
				   autocomplete="off" style="width:auto;vertical-align: top;">
			<input type = "button" id="code" οnclick="createCode()" style="width:40%;height:50px;font-size:20px;"/>
			<hr class="hr15">
			<button style="width: 100%;" type="submit"
				onclick="login(this)">登录</button>
			<hr class="hr20">
			<span id="info" style="color: red"></span>
		</form>


	</div>

</body>
<script src="js/libs/jquery-2.1.1.min.js"></script>
<script src="js/common.js"></script>
<script type="text/javascript">
	var checkInfo = false;

	if (top != self) {
		parent.location.href = '/login.html';
	}

	var user = loginInfo();
	if (user != "") {
		location.href = '/';
	}

	function login(obj) {
		validate();
		if (checkInfo){
			$(obj).attr("disabled", true);

			var username = $.trim($('#username').val());
			var password = $.trim($('#password').val());
			if (username == "" || password == "") {
				$("#info").html('用户名或者密码不能为空');
				$(obj).attr("disabled", false);
			} else {
				$.ajax({
					type : 'post',
					url : '/sys/login',
					data : $("#login-form").serialize(),
					success : function() {
						location.href = '/';
					},
					error : function(xhr, textStatus, errorThrown) {
						var msg = xhr.responseText;
						var response = JSON.parse(msg);
						$("#info").html(response.message);
						$(obj).attr("disabled", false);
					}
				});
			}
		}

	}

	 var code ; //在全局定义验证码
	//产生验证码
	window.onload = function createCode(){
		 code = "";
		 var codeLength = 4;//验证码的长度
		 var checkCode = document.getElementById("code");
		 var random = new Array(0,1,2,3,4,5,6,7,8,9,'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R',
		 'S','T','U','V','W','X','Y','Z');//随机数
		 for(var i = 0; i < codeLength; i++) {//循环操作
			var index = Math.floor(Math.random()*36);//取得随机数的索引（0~35）
			code += random[index];//根据索引取得随机数加到code上
		}
		checkCode.value = code;//把code值赋给验证码
	}
	//校验验证码
	function validate(){
		var inputCode = document.getElementById("checkCode").value.toUpperCase(); //取得输入的验证码并转化为大写
		if(inputCode.length <= 0) { //若输入的验证码长度为0
			$("#info").html('请输入验证码！');
		}
		else if(inputCode != code ) { //若输入的验证码与产生的验证码不一致时
			$("#info").html('验证码输入错误！@_@');
			createCode();//刷新验证码
			document.getElementById("input").value = "";//清空文本框
		}
		else { //输入正确时
			checkInfo = true;
		}
	}
</script>
</html>